package com.faithbj.schedule;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Abstract job object, which has consider the multiple thread synchronize problem.
 * 
 * @author chen liu
 */
public abstract class AbstractJob {
    protected Log log = LogFactory.getLog(this.getClass());
    private final Object monitor = new Object();
    private Boolean isRunning = false;

    /**
     * The entrance function of the job.
     */
    public void execute() {
        synchronized (this.monitor) {
            if (isRunning) {
                log.warn(this.getClass().getName() + " already running...");
                return;
            }
            isRunning = true;
        }

        try {
            executeInternal();
        } catch (Exception e) {
            log.error(e, e);
        } finally {
            synchronized (this.monitor) {
                isRunning = false;
            }
        }
    }

    /**
     * The internal function which focus on the business logic.
     * 
     * @throws Exception d
     */
    protected abstract void executeInternal() throws Exception;
}
